package org.ysh.core.attachment;

import org.ysh.annotation.AdditionInfoResolver;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;

@AdditionInfoResolver(extensions = {"xlsx"})
public class Excel2007AdditionInfoResolver implements AttachmentAdditionInfoResolver {

    @Override
    public void addAdditionInfo(File tmpFile, AdditionInfo additionInfo) throws Exception {
        try(XSSFWorkbook workbook = new XSSFWorkbook(tmpFile)){
            PoiHelper.writeCustomInfo(workbook.getProperties().getCustomProperties(),additionInfo);
        }

    }

    @Override
    public AdditionInfo readAdditionInfo(InputStream in) throws Exception {
        try(XSSFWorkbook workbook = new XSSFWorkbook(in)){
            return PoiHelper.readAdditionInfo(workbook.getProperties().getCustomProperties());
        }
    }

    public static void main(String[] args) throws Exception {
        Excel2007AdditionInfoResolver excel2007AdditionInfo = new Excel2007AdditionInfoResolver();
        AdditionInfo additionInfo = new AdditionInfo();
        additionInfo.setUserName("Lisssi");
        additionInfo.setTime(new Date());
        additionInfo.setHost("192.168.1.1");
        String filePath = "C:\\Users\\hxs_2\\Desktop\\南京海关\\1111.xlsx";
        excel2007AdditionInfo.addAdditionInfo(new File(filePath),additionInfo);

        additionInfo = excel2007AdditionInfo.readAdditionInfo(Files.newInputStream(Paths.get(filePath)));
        System.out.println(additionInfo);
    }
}
